package com.ns.scs.stat.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.sentinel.LimitType;
import com.ns.core.sentinel.RateLimiter;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.stat.bo.StatBO;
import com.ns.scs.stat.bo.StatPageBO;
import com.ns.scs.stat.dto.StatPageDto;
import com.ns.scs.stat.entry.StatEntry;
import com.ns.scs.stat.service.IStatService;
import com.ns.scs.stat.vo.StatVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 统计看板-业务接口
 *
 * @author liao.wei
 * @since 2024-05-16
 */
@RestController
@RequestMapping("/statistics")
@RequiredArgsConstructor
@CrossOrigin
public class StatController {

    private final IStatService statisticsService;

    /**
     * 新增
     *
     * @param bo 参数对象
     * @return RoleVo
     */
    @PostMapping("add")
    @ReqLog(module = "面板数据模块", name = "增")
    public ResultOk add(@RequestBody StatBO bo) {
        StatEntry statEntry = new StatEntry();
        BeanUtils.copyProperties(bo, statEntry);
        statEntry.setSort(statisticsService.count() + 1);
        return ResultOk.getByBoolean(statisticsService.saveOrUpdate(statEntry));
    }

    /**
     * 更新
     *
     * @param bo 参数对象
     * @return RoleVo
     */
    @PostMapping("/update")
    @ReqLog(module = "面板数据模块", name = "改")
    public ResultOk update(@RequestBody @Validated StatBO bo) {
        StatEntry statEntry = new StatEntry();
        BeanUtils.copyProperties(bo, statEntry);
        return ResultOk.getByBoolean(statisticsService.updateById(statEntry));
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "面板数据模块", name = "逻辑删")
    ResultOk delete(Long id) {
        return ResultOk.getByBoolean(statisticsService.removeById(id));
    }

    /**
     * 批量删除
     *
     * @param dto 参数
     * @return ResultOk
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "面板数据模块", name = "批量删")
    public ResultOk deleteByIds(@RequestBody IdsBO dto) {
        return ResultOk.getByBoolean(statisticsService.removeBatchByIds(dto.getIds()));
    }

    /**
     * 查询详情
     *
     * @param id 参数
     * @return ResultOk
     */
    @GetMapping("getById")
    @ReqLog(module = "面板数据模块", name = "详情")
    public ResultOk getById(Long id) {
        StatEntry statEntry = statisticsService.getById(id);
        StatVO vo = new StatVO();
        BeanUtils.copyProperties(statEntry, vo);
        return ResultOk.getSuccessData(vo);
    }

    /**
     * 分页查询
     *
     * @param bo 参数
     * @return Result
     */
    @PostMapping("getList")
    @ReqLog(module = "面板数据模块", name = "查-集合")
    public ResultOk getList(@RequestBody @Validated StatPageBO bo) {
        StatPageDto dto = new StatPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<StatVO> page = this.statisticsService.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }

    /**
     * 组装字典
     *
     * @param id 主键
     * @return Result
     */
    @GetMapping("sync")
    @ReqLog(module = "面板数据模块", name = "字典")
    public ResultOk syncStat(Long id) {
        return ResultOk.getByBoolean(this.statisticsService.syncStat(id));
    }

    /**
     * 组装字典
     *
     * @return Result
     */
    @GetMapping("options")
    @ReqLog(module = "面板数据模块", name = "字典")
    @RateLimiter(time = 5, count = 15, limitType = LimitType.IP)
    public ResultOk getOptions() {
        return ResultOk.getSuccessData(this.statisticsService.getOptions());
    }

}